package org.crazyrunner.module.sys.service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.crazyrunner.common.dao.HibernateBaseDao;
import org.crazyrunner.common.service.BaseService;
import org.crazyrunner.module.sys.entity.Menu;
import org.crazyrunner.module.sys.entity.SysUser;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

@Service("menuService")
public class MenuService extends BaseService{
	@Resource
	private HibernateBaseDao<Menu> hibernateBaseDao;

	public void save(Menu menu) {
		menu = new Menu();
		menu.setId("4");
		menu.setName("菜单三");
		SysUser user = new SysUser();
		user.setId("0");
		menu.setCreateBy(user);
		menu.setCreateDate(new Date());
		menu.setTarget(null);
		Menu parentMenu = new Menu();
		parentMenu.setId("1");
		menu.setParent(parentMenu);
		menu.setIsShow("0");
		menu.setParent(hibernateBaseDao.get(Menu.class,menu.getParent().getId()));
		menu.setParentIds(menu.getParent().getParentIds()+menu.getParent().getId()+",");
		hibernateBaseDao.save(menu);
	}
	
	public void deleteById(Serializable id){
		hibernateBaseDao.deleteById(Menu.class, id);
	}
	
	public Menu getMenu(Serializable id){
		return hibernateBaseDao.get(Menu.class, id);
	}
	
	public void deleteAll(){
		List list =hibernateBaseDao.findAll(DetachedCriteria.forClass(Menu.class));
		hibernateBaseDao.deleteAll(list);
	}
}
